/**
 * 人才库-黑名单列表(宽/窄)
 */
import React from 'react';
import { Icon as AntdIcon, Row, Table, Tooltip } from 'antd';
import { TableFilter } from '@/components';
import { getDict, getDisplayText } from '@/utils/dataDictUtil';
import { SEX, BLOCK_STAGE_TYPE } from '@/pages/Recruitment/dictionary';
import { Operates } from '@/pages/Recruitment/components';
import { history } from 'umi'
import styles from '@/pages/Recruitment/TalentsManagement/index.less';
import authHelper from '@/utils/auth/helper';
import { TalentMajorInfoCard } from '../../containers/index';
import { TalentsBlackListOperate } from '../../containers/operate';

import {
    renderLastPost,
    renderNameWithReadFlag,
    renderNameWithReadFlagTitle,
    renderShortDate,
    renderTalentDegree
} from '../../renderUtil';

const { checkCode } = authHelper;

const ResumeCardTable = ({ loading, dataSource, condition, onJumpDetail, onRefresh, currTab, pageType, ...rest }) => {
    const [agreeTypeList, setAgreeTypeList] = React.useState([]); // 学历
    // 字典获取
    React.useEffect(() => {
        getDict('agreeType').then(ret => setAgreeTypeList(ret));
    }, []);
    const onJumpDemandDetail = record => {
        const { recruitId, resumeStep } = record;
        // resumeStep: 1:简历 2：候选 3：面试 4：录用
        const activeKeys = {
            1: '2',
            2: '3',
            3: '4',
            4: '5'
        };
        history.push({
            pathname: '/office/personnelManagement/recruitment/demand/detail',
            query: {
                id: recruitId,
                activeKey: activeKeys[resumeStep] || '2'
            }
        });
    };
    const columns = [
        currTab === '1'
            ? {
                  title: <span style={{ marginLeft: '16px' }}>姓名</span>,
                  dataIndex: 'talentName',
                  render: (_, record) => (
                      <div style={{ cursor: 'pointer' }}>
                          <TalentMajorInfoCard talent={record} />
                      </div>
                  )
              }
            : {
                  title: renderNameWithReadFlagTitle('姓名'),
                  dataIndex: 'readFlag',
                  render: (value, record) => {
                      const { talentName = '--', showReadFlag } = record;
                      return renderNameWithReadFlag(talentName, value, showReadFlag);
                  }
              },
        ...(currTab === '2'
            ? [
                  {
                      title: '性别',
                      dataIndex: 'talentSex',
                      width: 80,
                      filteredValue: condition.talentSex ? [condition.talentSex] : [],
                      filters: SEX,
                      filterMultiple: false,
                      filterDropdown: filterProps => <TableFilter {...filterProps} />,
                      render: sex => getDisplayText(SEX, sex)
                  },
                  {
                      title: '学历',
                      dataIndex: 'talentDegree',
                      width: 100,
                      filteredValue: condition.talentDegree ? [condition.talentDegree] : [],
                      filters: agreeTypeList,
                      filterMultiple: false,
                      filterDropdown: filterProps => <TableFilter {...filterProps} />,
                      render: (val, record) =>
                          renderTalentDegree(getDisplayText(agreeTypeList, val), record.talentEduDetailVOList)
                  },
                  {
                      title: '最近工作',
                      dataIndex: 'lastPost',
                      render: (val, record) => renderLastPost(val, record.talentWorkDetailVOList)
                  }
              ]
            : []),
        {
            title: '应聘岗位',
            dataIndex: 'dutyName',
            onCell: () => ({
                onClick: e => {
                    e.stopPropagation();
                }
            }),
            render: (text, record) => {
                if (text && record.dutyCode) {
                    return checkCode('office:recruitment:course') ? (
                        <span
                            type="link"
                            className={styles.link}
                            onClick={() => {
                                onJumpDemandDetail(record);
                            }}
                        >
                            {text}
                        </span>
                    ) : (
                        text
                    );
                }
                return (
                    <Row align="middle" justify="center">
                        <span style={{ marginRight: '4px', color: 'red' }}>{text || '未匹配'}</span>
                        <Tooltip title={record.dutyCode ? '未匹配到招聘岗位' : '未匹配'}>
                            <AntdIcon type="question-circle" />
                        </Tooltip>
                    </Row>
                );
            }
        },
        {
            title: '拉黑时间',
            dataIndex: 'blockDate',
            sortOrder: condition.orderByFieldActive,
            sorter: () => {},
            render: val => renderShortDate(val)
        },
        {
            title: '拉黑阶段',
            dataIndex: 'resumeStages',
            filteredValue: condition.resumeStages || [],
            filters: BLOCK_STAGE_TYPE,
            filterMultiple: true,
            render: (_, record) => getDisplayText(BLOCK_STAGE_TYPE, record.blockStageId)
        },
        {
            title: '拉黑原因',
            dataIndex: 'blockTypeName'
        },
        {
            title: '操作',
            dataIndex: 'action',
            width: 180,
            onCell: () => ({
                onClick: e => {
                    e.stopPropagation();
                }
            }),
            render: (_, record) => {
                const { recruitId, talentCode } = record;
                const defaultNoticeEmployee = [];
                record.recruitList.forEach(item => {
                    defaultNoticeEmployee.push(item.launchEmployeeCode);
                    defaultNoticeEmployee.push(item.principalEmployeeCode);
                });
                const operates = [
                    <TalentsBlackListOperate
                        talentCode={talentCode}
                        operateType="remove"
                        onRefresh={onRefresh}
                        defaultNoticeEmployee={[...new Set([...defaultNoticeEmployee])]}
                        recruitId={recruitId}
                    />
                ];
                return <Operates operates={operates} />;
            }
        }
    ];
    return (
        <Table
            {...rest}
            rowKey="id"
            bordered={false}
            columns={columns}
            loading={loading}
            dataSource={dataSource}
            pagination={false}
            onRow={record => ({
                onClick: () => {
                    const { readFlag, talentCode } = record;
                    if (onJumpDetail) onJumpDetail(readFlag, talentCode);
                }
            })}
        />
    );
};
export default ResumeCardTable;
